package com.managedBean.cart;

import java.util.Date;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.dto.cart.CartDTO;
import com.dto.cart.CartOrderDTO;
import com.managedBean.registration.RegistrationManagedBean;
import com.utils.JsfUtils;
import com.utils.SendMailSSL;
import com.utils.SendMailTLS;

@ManagedBean(name = "prepareMail")
@ViewScoped
public class PrepareMail {
	private StringBuffer mailBody;
	private RegistrationManagedBean registrationManagedBean;

	
	public  PrepareMail() {
		registrationManagedBean = (RegistrationManagedBean) JsfUtils
				.getBeanInstanceFromFacesContext("registration");
		// Header template can be moved to properties file
		mailBody = new StringBuffer(
				"<table  style=&quot;width:100%;&quot; > <tbody>"
						+ " 	<tr>"
						+ "  			<td >"
						+ "  				<img src=&quot;/PF/images/logo.png&quot; style=&quot;width:25%;&quot; />"
						+ " 			</td>"
						+ " 			<td >"
						+ " 				<span style=&quot;float:right; width:75%;&quot;>"
						+ "                  	Product Company Name   <br/>"
						+ "				        Hyderabad - 500624, Andhra Pradesh, India.<br/>"
						+ "        		        Telephone No. : 9999999999 <br/>"
						+ "  					e-Mail : bmahesh.nitd@gmail.com <br/>"
						+ "				        Homepage : http://www.comapnywebsite.com <br/>"
						+ " 				</span>" + " 			</td>"
						+ "		</tr>	</tbody></table>");
	}

	public void sendMail(CartOrderDTO orderDTO, int id) {
		prepareMailBody(orderDTO, id);
		SendMailSSL mailSSL=new SendMailSSL();
		mailSSL.sendMail("bmahesh.nitd@gmail.com", registrationManagedBean.getEmail(), "Thank You for you Order "+id, mailBody.toString());
	}

	public void prepareMailBody(CartOrderDTO orderDTO, int id) {
		mailBody.append("<table style=&quot;width:100%;&quot; > 	<tbody>");
		mailBody.append("<tr> <td > <span > Order No :</span> 	</td> ");
		mailBody.append("<td>" + id + "</td>");
		mailBody.append("<td > <span > Address :</span> 	</td> ");
		mailBody.append("<td> " + registrationManagedBean.getHouseNo()
				+ "</td> </tr>");
		
		mailBody.append("<tr> <td > <span > Name of the Person :</span> 	</td> ");
		mailBody.append("<td>" + registrationManagedBean.getName() + "</td>");
		mailBody.append("<td >  	</td> ");
		mailBody.append("<td> " +registrationManagedBean.getStreet()+ registrationManagedBean.getArea()
				+ "</td> </tr>");
		
		mailBody.append("<tr> <td > <span > Date Of Order </span> 	</td> ");
		mailBody.append("<td>" + new Date() + "</td>");
		mailBody.append("<td > 	</td> ");
		mailBody.append("<td> " + registrationManagedBean.getLandmark()
				+ "</td> </tr>");
		mailBody.append("</tbody> </table>");
		
		mailBody.append("<div > <div > <table > " +
				"	<thead > <tr ><th >	<span>S.No</span> </th>" +
				"       		  <th >	<span>Name</span> </th>" +
				"                 <th >	<span>Quantty</span> </th>" +
				"  				  <th >	<span>Price</span>   </th>" +
				"   		</tr>" +
				"	</thead>");
		mailBody.append("<tbody >");
		for(CartDTO var:orderDTO.getItemsList()){
			mailBody.append("<tr>");
			mailBody.append("<td></td>");
			mailBody.append("<td>"+var.getBrand()+" " +var.getItemName()+" " +var.getSize()+"</td> </tr>");
			mailBody.append("<td>"+var.getPrice()+"</td> </tr>");
		}
		mailBody.append("</tbody> </table> </div> </div> ");
		mailBody.append("<table > <tbody> " +
									"<tr> " +
									"	<td> <label >Shipping:</label> </td> " +
									"	<td><span >"+orderDTO.getShippingCharges()+ "</span></td> " +
									"</tr>" +
									"<tr>" +
									"	<td><label >Sub Total:</label></td>" +
									"	<td><span >"+orderDTO.getSubTotal()+ "</span></td>" +
									"</tr>" +
									"<tr>" +
									"	<td><label >Total Amount Payable:</label></td>" +
									"	<td><span >"+orderDTO.getTotal()+ "</span></td>" +
									"</tr></tbody></table>");

	}
}
